package kz.edu.sdu.atm.client;

import java.io.IOException;
import java.math.BigDecimal;

import kz.edu.sdu.atm.client.ui.IDisplay;
import kz.edu.sdu.atm.models.OperationType;
import kz.edu.sdu.atm.models.Request;
import kz.edu.sdu.atm.models.Response;
import kz.edu.sdu.atm.models.ResponseStatus;

public class Client implements IClient {
	String serverHost;
	int serverPort;
	IDisplay display;

	public Client(String host, int port) {
		serverHost = host;
		serverPort = port;
	}

	public void showLoading(boolean ok) {
		if (display != null) {
			display.showLoading(ok);
		}
	}

	public Response sendRequest(Request req) {
		showLoading(true);
		SpeakThread r = new SpeakThread(req, serverHost, serverPort);
		r.run();
		// Thread t = new Thread(r);
		// t.start();
		// while (t.getState().equals(Thread.State.TERMINATED)) {
		// }
		showLoading(false);
		return r.getResponse();
	}

	@Override
	public boolean auth(String username, String password) {
		Request r = new Request();
		r.setUsername(username);
		r.setPassword(password);
		r.setOperation(OperationType.AUTH);
		Response res = sendRequest(r);
		if (res != null) {
			if (res.getStatus().equals(ResponseStatus.OK))
				return true;
		}
		return false;
	}

	@Override
	public BigDecimal balance(String username, String password)
			throws IOException {
		Request r = new Request();
		r.setUsername(username);
		r.setPassword(password);
		r.setOperation(OperationType.BALANCE);
		Response res = sendRequest(r);
		if (res != null) {
			if (res.getStatus().equals(ResponseStatus.OK))
				return new BigDecimal(res.getMessage());
		}
		throw new IOException(res.toString());
	}

	@Override
	public BigDecimal deposit(String username, String password,
			BigDecimal amount) throws IOException {
		Request r = new Request();
		r.setUsername(username);
		r.setPassword(password);
		r.setOperation(OperationType.DEPOSIT);
		r.getMap().put("DEPOSIT", amount.toPlainString());
		Response res = sendRequest(r);
		if (res != null) {
			if (res.getStatus().equals(ResponseStatus.OK))
				return new BigDecimal(res.getMessage());
		}
		throw new IOException(res.toString());
	}

	@Override
	public BigDecimal send(String username, String password, String to,
			BigDecimal amount) throws IOException {
		Request r = new Request();
		r.setUsername(username);
		r.setPassword(password);
		r.setOperation(OperationType.SEND);
		r.getMap().put("SEND", amount.toPlainString());
		r.getMap().put("TO", to);
		Response res = sendRequest(r);
		if (res != null) {
			if (res.getStatus().equals(ResponseStatus.OK))
				return new BigDecimal(res.getMessage());
		}
		throw new IOException(res.toString());
	}

	@Override
	public BigDecimal withdraw(String username, String password,
			BigDecimal amount) throws IOException {
		Request r = new Request();
		r.setUsername(username);
		r.setPassword(password);
		r.setOperation(OperationType.WITHDRAW);
		r.getMap().put("WITHDRAW", amount.toPlainString());
		Response res = sendRequest(r);
		if (res != null) {
			if (res.getStatus().equals(ResponseStatus.OK))
				return new BigDecimal(res.getMessage());
		}
		throw new IOException(res.toString());
	}

	public IDisplay getDisplay() {
		return display;
	}

	public void setDisplay(IDisplay display) {
		this.display = display;
	}

}
